<?php

namespace App\Admin\Controllers;

use App\Lib\Uploads\UploadHandler;
use App\Models\WxAttachment;
use Dcat\Admin\Traits\HasUploadedFile;
use Illuminate\Http\Request;


/**
 * 后台自定义上传接口
 */
class FileController
{
    use HasUploadedFile;

    public function handle()
    {



        // 判断是否是删除文件请求
        if ($this->isDeleteRequest()) {
            $disk = $this->disk('local');
            // 删除文件并响应
            return $this->deleteFileAndResponse($disk);
        }

        $file = $this->file();
        $result = UploadHandler::handle($file);

//        $filePath = $this->file();
//        $storePath = date('Y/m/d',time());
//        $disk = config('admin.upload.disk');
//        $path = $filePath->store($storePath,$disk);
//        $domain = config('filesystems.disks.'.$disk.'.domains.https');
//        if (empty($model)) {
//            $model = new WxAttachment();
//            $model->path = $path;
//            $result = $model->save();
//        }


        return $result
            ? ['data'=>[
                'id'=>$result['url'],
                'name'=>$result['name'],
                'path'=>$result['path'],
                'url'=>$result['url']
            ],'status'=>true]
            : $this->responseErrorMessage('文件上传失败');
    }
}
